﻿using UnityEngine;
using System.Collections;

public class AIController : MonoBehaviour {
	//This script is not used at the moment.
	public Vector3 startPosition;
	public Vector3 endPosition;
	public Vector3 currentGoalPosition;

	public GridCell initialGridCell;
	public GridCell goalGridCell;
	public enum movingDirection {notMoving, movingUp, movingDown, movingRight, movingLeft}
	public movingDirection currentDirection;

	public Animator anim;

	public Vector3 directionalVector = new Vector3(0,-1,0);
	public Vector3 distanceVector = new Vector3(0,0,0);
	public float speed;
	public float radius;
	// Use this for initialization
	void Start () {
		//initializePosition
		transform.position = new Vector3(initialGridCell.transform.position.x, initialGridCell.transform.position.y, transform.position.z);
		startPosition = transform.position;
		endPosition = new Vector3(goalGridCell.transform.position.x, goalGridCell.transform.position.y, transform.position.z);
		currentGoalPosition = endPosition;
		anim = GetComponent<Animator>();
		anim.SetBool("isMoving", true);
		speed = 1f;
		radius = .15f;
	}
	
	// Update is called once per frame
	void Update () {
		distanceVector = (transform.position - currentGoalPosition);
		if((transform.position.y - currentGoalPosition.y) < -radius){
			currentDirection = movingDirection.movingUp;
		}
		else if((transform.position.y - currentGoalPosition.y) > radius){
			currentDirection = movingDirection.movingDown;
		}
		else if((transform.position.x - currentGoalPosition.x) > radius){
			currentDirection = movingDirection.movingLeft;
		}
		else if((transform.position.x - currentGoalPosition.x) < -radius){
			currentDirection = movingDirection.movingRight;
		}
		if(distanceVector.magnitude < radius){
			if(currentGoalPosition == startPosition){
				currentGoalPosition = endPosition;
			}
			else{
				currentGoalPosition = startPosition;
			}

		}
	}

	void FixedUpdate(){
		if(currentDirection == movingDirection.movingUp){
			anim.SetBool("isMovingUp", true);
			anim.SetBool ("isMovingDown", false);
			directionalVector = new Vector3(0,1,0);
		}
		else if(currentDirection == movingDirection.movingDown){
			anim.SetBool("isMovingUp", false);
			anim.SetBool ("isMovingDown", true);
			directionalVector = new Vector3(0,-1,0);
		}
		else if(currentDirection == movingDirection.movingRight){
			anim.SetBool("isMovingLeft", false);
			anim.SetBool ("isMovingRight", true);
			directionalVector = new Vector3(1,0,0);
		}
		else if(currentDirection == movingDirection.movingLeft){
			anim.SetBool("isMovingLeft", true);
			anim.SetBool ("isMovingRight", false);
			directionalVector = new Vector3(-1,0,0);
		}
		performMove();
	}

	void performMove(){
		transform.position += directionalVector * speed * Time.deltaTime;
	}

	void OnTriggerEnter(Collider other){
		if(other.gameObject.CompareTag("gridcell")){
			GridCell gridcell = other.gameObject.GetComponent<GridCell>();
			gridcell.walkable = false;
			if(GameController.player.destinationGridCell != null){
				GameController.gc.setPlayerTarget(GameController.player.destinationGrid, GameController.player.destinationGridCell);
			}
		}
		if(other.gameObject.CompareTag("player")){
			speed = 0f;
		}
	}

	void OnTriggerExit(Collider other){
		if(other.gameObject.CompareTag("gridcell")){
			GridCell gridcell = other.gameObject.GetComponent<GridCell>();
			gridcell.walkable = true;
		}
		if(other.gameObject.CompareTag("player")){
			speed = 1f;
		}
	}
}
